var unitType = 1;
var unitTypeName = '';

var roleType,unitId,userId;
//分页初始化
var pageNum = 1;
var pageSize = 10;
var total;

mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		},
		up: { //上拉加载
			contentinit: '',
			contentrefresh: '正在加载...',
			contentnomore: '没有更多了',
			callback: pullupRefresh,
		}
	}
});

mui.plusReady(function() {
	userId = localStorage.getItem("userId");
	unitId = localStorage.getItem("unitId");
	roleType = Number(localStorage.getItem("roleType"));
	self = plus.webview.currentWebview();
//	alert(self.info.unitType);
		
	unitType = self.info.unitType;
	unitTypeName = self.info.unitTypeName;
	
	document.getElementById('navName').innerText = unitTypeName + '系统';
	
	loadData()
	
    //显示跳转链接
	mui('#list_wrap').on('tap','li',function(){
		var id=this.getAttribute('data-id');
		openNew("alarmList.html", {
			alarmUnitId: id,
			unitType: unitType
		});
	})
	//点击搜索
	/*document.getElementById('search').addEventListener('tap',function(){
		openNew("../widget/search.html");
	})*/
	
})

//下拉刷新具体业务实现
function pulldownRefresh() {
	//重置页码
	pageNum = 1;
	loadData();
}
// 上拉加载具体业务实现
function pullupRefresh() {
	loadData(true);
}

//一次性拉取数据
function loadData(isnextpage, isreload) {
	if(isnextpage&&(pageNum * pageSize < total)) { //加载下一页
		pageNum++;
	} else if(isreload) { //重新加载当前页
		pageNum = curr_pageno;
	} else if(pageNum == 0) {
		pageNum = 1; //未加载过
	} else {
		pageNum = 1; //默认加载第一页
	}
	var showload = pageNum == 1;
	var isappend = pageNum > 1 ? true : false;
	log(pageNum + "," + total + "," + showload + "," + isappend)
	if(pageNum*pageSize >= total) {
		appPage.endPullRefresh(true);
	}

	request("/app/alarm/select/center/unit/list/" + unitType, "GET", {
		roleType: roleType,
		unitId: unitId,
		id: userId,
		pageNum: pageNum,
		pageSize: pageSize
	}, function(res) {
		pageNum = res.pager.pageNum;
		pageSize = res.pager.pageSize;
		total = res.pager.total;
		
		var nomore = true;
		if(res.code == 0) {
			render("#list_wrap", "unitList", res, isappend);
			nomore = pageNum*pageSize >= total;
		} else {
			appUI.showTopTip(json.msg);
		}
		appPage.endPullRefresh(nomore);
	}, true, function() {
		appPage.endPullRefresh();
	});
}
